﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.QuanLyKho;

namespace QuanLyKho.Control
{
    public partial class Kho_Xuatkho : UserControl
    {
        public Kho_Xuatkho()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvXuatkho.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvXuatkho.OptionsView.ShowFooter = true;
            gvXuatkho.OptionsFind.AlwaysVisible = true;
            gcID.Visible = false;

            gvDuyet.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvDuyet.OptionsView.ShowFooter = true;
            gvDuyet.OptionsFind.AlwaysVisible = true;
            gcID1.Visible = false;
        }

        private void LoadData()
        {
            gridXuatkho.DataSource = Data.DataAll.SelectAll("XuatKho_VatTu inner join Kho_KhoHang on XuatKho_VatTu.[MaPhong] = Kho_KhoHang.[IDKhoHang]",
                "XuatKho_VatTu.TrangThai,Kho_KhoHang.[TenKho] as MaPhong,XuatKho_VatTu.[ID],XuatKho_VatTu.[NgayXuat],XuatKho_VatTu.[NguoiLapPhieu],XuatKho_VatTu.[NgayChungTu],XuatKho_VatTu.[NgayHoachToan],XuatKho_VatTu.[SoChungTu],XuatKho_VatTu.[GhiChu],XuatKho_VatTu.[NguoiNhan]",
                "WHERE TrangThai= " + Convert.ToInt32(TrangThaiKho.XuatKho_TaoMoi) + "OR TrangThai = " + Convert.ToInt32(TrangThaiKho.XuatKho_KhongDuyet));
            gridDuyet.DataSource = Data.DataAll.SelectAll("XuatKho_VatTu inner join Kho_KhoHang on XuatKho_VatTu.[MaPhong] = Kho_KhoHang.[IDKhoHang]",
                "XuatKho_VatTu.TrangThai,Kho_KhoHang.[TenKho] as MaPhong,XuatKho_VatTu.[ID],XuatKho_VatTu.[NgayXuat],XuatKho_VatTu.[NguoiLapPhieu],XuatKho_VatTu.[NgayChungTu],XuatKho_VatTu.[NgayHoachToan],XuatKho_VatTu.[SoChungTu],XuatKho_VatTu.[GhiChu],XuatKho_VatTu.[NguoiNhan]",
                "WHERE TrangThai= " + Convert.ToInt32(TrangThaiKho.XuatKho_ChoDuyet) + "OR TrangThai = " + Convert.ToInt32(TrangThaiKho.XuatKho_DaDuyet));

        }

        private void gvXuatkho_DoubleClick(object sender, EventArgs e)
        {
            Kho_Themxuatkho.ID_XuatKho = gvXuatkho.Columns["ID"].View.GetRowCellDisplayText(gvXuatkho.FocusedRowHandle, "ID");
            Kho_Themxuatkho themnhapkho = new Kho_Themxuatkho();
            themnhapkho.ShowDialog();
            LoadData();
            Kho_Themxuatkho.ID_XuatKho = "0";
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            Kho_Themxuatkho themnhapkho = new Kho_Themxuatkho();
            themnhapkho.ShowDialog();
            LoadData();
        }

        private void btnGuiPhieu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvXuatkho.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi lệnh xuất kho này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("XuatKho_VatTu", "TrangThai =" + Convert.ToInt32(TrangThaiKho.XuatKho_ChoDuyet) + " WHERE ID =" + dataview[gvXuatkho.FocusedRowHandle]["ID"]);

                MessageBox.Show(message.Equals("success") ? "Gửi lệnh thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void gvDuyet_DoubleClick(object sender, EventArgs e)
        {
            Kho_Themxuatkho.ID_XuatKho = gvDuyet.Columns["ID"].View.GetRowCellDisplayText(gvDuyet.FocusedRowHandle, "ID");
            Kho_Themxuatkho themxuatkho = new Kho_Themxuatkho();
            themxuatkho.ShowDialog();
            LoadData();
            Kho_Themxuatkho.ID_XuatKho = "0";
        }

        private void gvXuatkho_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiKho(Convert.ToInt32(e.Value));
            }
        }

        private void gvDuyet_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiKho(Convert.ToInt32(e.Value));
            }
        }
    }
}
